package com.example.mqtt.mica.job;

import com.example.mqtt.mica.mqtt.producer.MqttProducer;
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @author ghl
 * @date 2023/11/16 12:05
 * @note
 */
@Component
public class JobSendMsg {

    private static final Logger logger = LoggerFactory.getLogger(JobSendMsg.class.getName());

    @Autowired
    private MqttProducer mqttProducer;


    @Scheduled(fixedRate = 5000)
    public void sendMsg() {
        //构造随机topic
        String topic = "/mqttdemo/" + System.currentTimeMillis();
        //消息内容
        String msg = "mica - " + UUID.randomUUID().toString();
        logger.info("随机发消息.topic:[{}].msg:[{}]", topic, msg);
        mqttProducer.publish(topic, msg.getBytes(), MqttQoS.AT_MOST_ONCE);
    }


}
